package com.taotao.sso.service.impl;

import java.util.Date;
import java.util.List;
import java.util.UUID;

import javax.persistence.Id;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.taotao.sso.model.TbUser;
import com.taotao.sso.model.TbUserDao;
import com.taotao.sso.service.TbUserService;

@Service
public class TbUserServiceImpl implements TbUserService {

	@Autowired
	private TbUserDao tbUserDao;

	@Override
	public boolean userCheck(String param, String type) {
		TbUser tbUser = null;
		// type为类型，可选参数1、2、3分别代表username、phone、email
		if (type.equals("1")) {
			// 1 username
			tbUser = tbUserDao.findByUsername(param);
		} else if (type.equals("2")) {
			// 2 phone
			tbUser = tbUserDao.findByPhone(param);
		} else if (type.equals("3")) {
			tbUser = tbUserDao.findByEmail(param);
		} else {
			tbUser = null;
		}
		if (tbUser != null) {
			return false;
		}
		return true;
	}

	@Override
	public void userRegister(TbUser tbUser) {

	}

	@Override
	public String userLogin(TbUser tbUser) {
		TbUser user = tbUserDao.findByUsernameAndPassword(tbUser.getUsername(), tbUser.getPassword());
		if (user != null) {
			return UUID.randomUUID().toString();
		} else {
			return "";
		}
	}

	@Override
	public TbUser userToken(String token) {
		return null;
	}

	@Override
	public String userLogout(String token) {
		return null;
	}

	@Override
	public List<TbUser> findAll() {
		List<TbUser> findAll = (List<TbUser>) tbUserDao.findAll();
		return findAll;
	}

	@Override
	public TbUser findByID(Long id) {
		return tbUserDao.findById(id);
	}

	@Override
	public TbUser save(TbUser tbUser) {
		tbUser.setCreated(new Date());
		tbUser.setUpdated(new Date());
		TbUser user;
		try {
			user = tbUserDao.save(tbUser);
		} catch (Exception e) {
			user = null;
		}
		return user;
	}

	@Override
	public TbUser update(Long id, TbUser tbUser) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String delete(Long id) {
		tbUserDao.delete(id);
		return "";
	}

}
